package com.yuyou.train.lesson.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.train.lesson.mapper.ResourceMapper;
import com.yuyou.train.lesson.service.ILessonResourceRuleRelationService;
import com.yuyou.train.lesson.service.ILessonService;
import com.yuyou.train.lesson.service.IResourceExamService;
import com.yuyou.train.lesson.service.IResourceService;
import com.yuyou.train.service.api.lesson.dto.ResourceDetailDTO;
import com.yuyou.train.service.api.lesson.dto.ResourcePageDTO;
import com.yuyou.train.service.api.lesson.entity.Lesson;
import com.yuyou.train.service.api.lesson.entity.LessonResourceRuleRelation;
import com.yuyou.train.service.api.lesson.entity.Resource;
import com.yuyou.train.service.api.lesson.entity.ResourceExam;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.enums.CommEnums;
import com.yuyou.train.util.message.lesson.LessonMess;
import com.yuyou.train.util.message.lesson.ResourceMess;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class ResourceServiceImpl implements IResourceService{

	@Autowired
	private ResourceMapper resourceMapper;
	
	@Autowired
	private ILessonService lessonService;
	
	@Autowired
	private ILessonResourceRuleRelationService lesResRuleRelService;
	
	@Autowired
	private IResourceExamService resourceExamService;
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return resourceMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Resource record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		return resourceMapper.insert(record);
	}

	@Override
	public ResBoolSimpleInfo<String> addOrUpdateRes(Resource record) {
		
		if(record.getResourceType().equals(CommEnums.resourceType.video.getValue())
				||record.getResourceType().equals(CommEnums.resourceType.music.getValue())){
			if(StringUtils.isEmpty(record.getResourceUrl())){
				return new ResBoolSimpleInfo<String>(false,ResourceMess.ERR_NOT_EMPTY_URL);
			}
			if(record.getTime()==null && record.getTime()<=0){
				return new ResBoolSimpleInfo<String>(false,ResourceMess.ERR_NOT_EMPTY_TIME);
			}	
		}
		if(record.getResourceType().equals(CommEnums.resourceType.exam.getValue())){
			if(StringUtils.isEmpty(record.getResourceExamId())){
				return new ResBoolSimpleInfo<String>(false,ResourceMess.ERR_NOT_EMPTY_EXAM_OR_NOUSE);
			}
			
			ResourceExam exam=resourceExamService.selectByPrimaryKey(record.getResourceExamId());
			if(exam==null || exam.getIsUse().equals(CommEnums.isUse.no.getValue())){
				return new ResBoolSimpleInfo<String>(false,ResourceMess.ERR_NOT_EMPTY_EXAM_OR_NOUSE);
			}
			
			
		}
		if(record.getResourceType().equals(CommEnums.resourceType.hwNeedComment.getValue())||
				record.getResourceType().equals(CommEnums.resourceType.hwJustCommit.getValue())){
			if(StringUtils.isEmpty(record.getTaskContent())){
				return new ResBoolSimpleInfo<String>(false,ResourceMess.ERR_NOT_EMPTY_TASK_CONTENT);
			}
		}		
		if(!StringUtils.isEmpty(record.getId())){
			ResBoolSimpleInfo<String> res=hasLessonRel(record.getId());
			if(!res.isSuccess()){
				return res;
			}
			this.updateByPrimaryKeySelective(record);
			
		}else{
			this.insert(record);
		}

		return ResBoolSimpleInfo.getSuccessInfo();
	}
	
	@Override
	public ResourceDetailDTO selectByPrimaryKey(String id) {
		return resourceMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(Resource record) {
		record.setUpdateTime(new Date());
		return resourceMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<ResourcePageDTO> findResPageList(Pagination<ResourcePageDTO> p) {
		return resourceMapper.findResPageList(p);
	}

	@Override
	public int findResPageCount(Pagination<ResourcePageDTO> p) {
		return resourceMapper.findResPageCount(p);
	}

	@Override
	public ResBoolSimpleInfo<String> delRes(String id) {
		
		//资源关联课程其他不能删除	
		ResBoolSimpleInfo<String> res=hasLessonRel(id);
		if(!res.isSuccess()){
			return res;
		}		
		this.deleteByPrimaryKey(id);		
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public List<Resource> findResourceByExamId(String examId) {
		return resourceMapper.findResourceByExamId(examId);
	}

	@Override
	public ResBoolSimpleInfo<String> hasLessonRel(String id){
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		LessonResourceRuleRelation lessResRuleRel=new LessonResourceRuleRelation();
		lessResRuleRel.setResourceId(id);
		List<LessonResourceRuleRelation> listRel=lesResRuleRelService.findLesResRuleRel(lessResRuleRel);
		if(listRel.size()>0){
			StringBuilder lesName=new StringBuilder();
			listRel.forEach(item -> {
				Lesson l=lessonService.selectByPrimaryKey(item.getLessonId());
				lesName.append(l.getName()+";");
				});
			res.setSuccess(false);
			res.setMessage(String.format(LessonMess.ERR_UPDATE_HAS_LESS_RULE, lesName.toString()));
			return res;
		}
		return ResBoolSimpleInfo.getSuccessInfo();
	}
			
	

//	public ResBoolSimpleInfo<String> isLessonUse(String id,List<LessonResourceRuleRelation> listRel){
//		LessonResourceRuleRelation lessResRuleRel=new LessonResourceRuleRelation();
//		lessResRuleRel.setResourceId(id);
//		if(listRel==null){
//			listRel=lesResRuleRelService.findLesResRuleRel(lessResRuleRel);
//		}
//		for(LessonResourceRuleRelation rel:listRel){
//			String lessonId=rel.getLessonId();
//			ResBoolSimpleInfo<String> res=lessonService.isLessonUse(lessonId);
//			if(!res.isSuccess()){
//				return res;
//			}
//		}
//		
//		
//		return ResBoolSimpleInfo.getSuccessInfo();
//	}


}